package com.lixin.rabbitmq.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitDeadConfig {

    // 创建DLX(Dead Letter Exchange)死信交换机(与正常交换机没有任何区别)
    @Bean
    public DirectExchange deadLetterExchange() {
        return new DirectExchange(
                "dead_letter_exchange", // 交换机名称
                true, // 交换机是否持久化
                false // 是否自动删除交换机
        );
    }

    // 创建死信队列
    @Bean
    public Queue deadLetterQueue() {
        return new Queue(
                "dead.letter.queue", // 队列名称
                true // 是否持久化
        );
    }

    // 将死信队列绑定到DLX
    @Bean
    public Binding deadLetterBinding() {
        return BindingBuilder.bind(deadLetterQueue()).to(deadLetterExchange()).with("dead");
    }

}
